<?php
/**
 * Created by PhpStorm.
 * User: hubudong
 * Date: 2020/7/18 0018
 * Time: 上午 9:55
 */
use Illuminate\Support\Facades\Redis;

if(!function_exists('redis_clear')) {
    /**
     * Redis 清除（管道版）
     *
     * @param string $prefix 前缀
     * @return bool
     */
    function redis_clear($prefix) {
        $keys = Redis::keys($prefix . '*');
        $_prefix = config('database.redis.options.prefix');
        $keys = array_chunk($keys, 1000);
        foreach($keys as $_keys) {
            Redis::pipeline(function($pipe) use($_keys, $_prefix) {
                foreach($_keys as $key) {
                    $pipe->del(str_replace($_prefix, '', $key));
                }
            });
        }
        return true;
    }
}

if (!function_exists('zero')){
    function zero(){
        return strtotime(date("Y-m-d"),time());
    }
}

if (!function_exists('date_assemble')){
    function date_assemble($start_time,$end_time){
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time) + 86400;
        return[$start_time,$end_time];
    }
}

if (!function_exists('get_proxy_ip')){
    function get_proxy_ip()
    {
        $arr_ip_header = array(
            'HTTP_CDN_SRC_IP',
            'HTTP_PROXY_CLIENT_IP',
            'HTTP_WL_PROXY_CLIENT_IP',
            'HTTP_CLIENT_IP',
            'HTTP_X_FORWARDED_FOR',
            'REMOTE_ADDR',
        );
        $client_ip = 'unknown';
        foreach ($arr_ip_header as $key)
        {
            if (!empty($_SERVER[$key]) && strtolower($_SERVER[$key]) != 'unknown')
            {
                $client_ip = $_SERVER[$key];
                break;
            }
        }
        return $client_ip;
    }
}

if (! function_exists('getOSSUrl')) {

    function getOSSUrl($path,$flag = false)
    {
         if(!empty($path)){
             $config = config('filesystems.disks.oss');


             $cdnDomain = empty($config['cdnDomain']) ? '' : $config['cdnDomain'];
             $bucket    = $config['bucket'] ?? '';
             $ssl       = empty($config['ssl']) ? false : $config['ssl'];
             $isCname   = empty($config['isCName']) ? false : $config['isCName'];
//        $debug     = empty($config['debug']) ? false : $config['debug'];

             $endPoint  = $config['endpoint'] ?? ''; // 默认作为外部节点
             $url = ( $ssl ? 'https://' : 'http://' ) . ( $isCname ? ( $cdnDomain == '' ? $endPoint :  $cdnDomain ) : $bucket . '.' . $endPoint ) . '/' . ltrim($path, '/');
//             $url = 'https://caidj-image.oss-cn-beijing.aliyuncs.com/'.ltrim($path, '/');

         }else{
             if($flag){
                 $url = $path ?? '';
             }else{
                 $url = config('app.default_img.item_default');
             }


         }


        return $url;
    }
}

if (! function_exists('num_to_rmb')) {
    /**
     * 数字转中文
     * @param $num 数字
     * @return string 转成中文
     */
    function num_to_rmb($num)
    {
        $c1 = "零壹贰叁肆伍陆柒捌玖";
        $c2 = "分角元拾佰仟万拾佰仟亿";
        //精确到分后面就不要了，所以只留两个小数位
        $num = round($num, 2);
        //将数字转化为整数
        $num = $num * 100;
        if (strlen($num) > 10) {
            return "金额太大，请检查";
        }
        $i = 0;
        $c = "";
        while (1) {
            if ($i == 0) {
                //获取最后一位数字
                $n = substr($num, strlen($num) - 1, 1);
            } else {
                $n = $num % 10;
            }
            //每次将最后一位数字转化为中文
            $p1 = substr($c1, 3 * $n, 3);
            $p2 = substr($c2, 3 * $i, 3);
            if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
                $c = $p1 . $p2 . $c;
            } else {
                $c = $p1 . $c;
            }
            $i = $i + 1;
            //去掉数字最后一位了
            $num = $num / 10;
    //            $num = (int)$num;
            $num = intval(substr($num,0));
            //结束循环
            if ($num == 0) {
                break;
            }
        }
        $j = 0;
        $slen = strlen($c);
        while ($j < $slen) {
            //utf8一个汉字相当3个字符
            $m = substr($c, $j, 6);
            //处理数字中很多0的情况,每次循环去掉一个汉字“零”
            if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
                $left = substr($c, 0, $j);
                $right = substr($c, $j + 3);
                $c = $left . $right;
                $j = $j - 3;
                $slen = $slen - 3;
            }
            $j = $j + 3;
        }
        //这个是为了去掉类似23.0中最后一个“零”字
        if (substr($c, strlen($c) - 3, 3) == '零') {
            $c = substr($c, 0, strlen($c) - 3);
        }
        //将处理的汉字加上“整”
        if (empty($c)) {
            return "零元整";
        } else {
            return $c . "整";
        }
    }





}

/**
 * http请求
 */
if(!function_exists('zc_curl')) {
    function zc_curl($url,$method='GET',$params=[],$header = []){
        //初始化CURL句柄
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if(!empty($header)){
            curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header );
        }
        //请求时间
        $timeout = 30;
        curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
        switch ($method){
            case "GET" :
                curl_setopt($curl, CURLOPT_HTTPGET, true);
                break;
            case "POST":
                if(is_array($params)){
                    $params = json_encode($params,320);
                }

                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($curl, CURLOPT_POSTFIELDS,$params);
                break;
        }
        $data = curl_exec($curl);
        curl_close($curl);//关闭cURL会话
        return $data;
    }
}
